package com.demo.config.security.jump;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>登入成功后跳转</p>
 *@author zwy
 *@date 2024/1/17 11:04
*/
public class AutenticationSuccess implements AuthenticationSuccessHandler {
    private ObjectMapper om=new ObjectMapper();
    /**
     * 登入成功后的操作
     * @param request
     * @param response
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        System.out.println("登入成功了");

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset-utf-8");
        String json=null;
        json=om.writeValueAsString(authentication);

        response.getWriter().write(json);
        response.getWriter().flush();

           response.sendRedirect("/index.html");

    }
}
